package modele;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DAO {
    public static final String url="http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
    public static List<String> getLesDeps(){
        URL myURL;
        try {
            List<String> MaListe = new ArrayList<String>();
            myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());
        
            Element racine = (Element) doc.getDocumentElement();
            
            NodeList listeDep = racine.getElementsByTagName("Departement");
            // recup des departements
            for(int i=0; i<listeDep.getLength(); i++){
                Node departement = listeDep.item(i);
                NodeList lesProprietes = departement.getChildNodes();
                //recherche du nom
                for(int j=0; j<lesProprietes.getLength(); j++){
                    if(lesProprietes.item(j).getNodeName().equals("num")){
                        MaListe.add(lesProprietes.item(j).getTextContent().trim());
                    }
                }
            }
            return MaListe;
        } catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public static final String url2="http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/";
    public static List<Medecin> getLesMeds(String dep){
        URL myURL2;
        try {
            List<Medecin> maListe = new ArrayList<Medecin>();
            myURL2 = new URL(url2 + dep);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL2.openStream());
        
            Element racine = (Element) doc.getDocumentElement();
            
            NodeList listeMed = racine.getElementsByTagName("Medecin");
            // recup des departements
            for(int i=0; i<listeMed.getLength(); i++){
                Node medecin = listeMed.item(i);
                NodeList lesProprietes = medecin.getChildNodes();
                //recherche du nom
                String nom = "";
                String prenom = "";
                String adresse = "";
                String spe = "";
                String tel = "";
                for(int j=0; j<lesProprietes.getLength(); j++){
                    if(lesProprietes.item(j).getNodeName().equals("nom")){
                        nom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if(lesProprietes.item(j).getNodeName().equals("prenom")){
                        prenom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if(lesProprietes.item(j).getNodeName().equals("adresse")){
                        adresse = lesProprietes.item(j).getTextContent().trim();
                    }
                    if(lesProprietes.item(j).getNodeName().equals("spe")){
                        spe = lesProprietes.item(j).getTextContent().trim();
                    }
                    if(lesProprietes.item(j).getNodeName().equals("tel")){
                        tel = lesProprietes.item(j).getTextContent().trim();
                    }
                }
                Medecin m = new Medecin(nom, prenom, adresse, spe, tel);
                maListe.add(m);
            }
            return maListe;
        } catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}